package com.honeymoondesert.crawler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.crawler.Crawler;
import com.google.gson.JsonObject;
import com.honeymoondesert.util.PatternHelper;

public class CommentsInfo {
	private String storeId;
	private String commentUrl;
	private String date;
	private Document doc;
	private int pageno = 1;
	public List<JsonObject> comments = new ArrayList<JsonObject>();

	public CommentsInfo() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date now = new Date();
		this.date = sdf.format(now);
	}

	public CommentsInfo(String commentUrl) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date now = new Date();
		this.date = sdf.format(now);
		this.commentUrl = commentUrl;
	}

	/**
	 * 获取某日期节点之前的所有点评
	 * 
	 * @param date
	 *            yyyy-mm-dd
	 * @return
	 */
	public List<JsonObject> getCommentsBeforeDate(String date) {
		Boolean stop = false;
		while (!stop) {
			Crawler commentCrawler = new Crawler(commentUrl + "?pageno="
					+ pageno);
			if (!commentCrawler.loadPage())
				return null;
			doc = commentCrawler.getDoc();
			String cssQuery = ".comment-list>ul>li";
			Elements elements = doc.select(cssQuery);
			for (Element element : elements) {
				JsonObject comment = fetchComment(element);
				// 如果在日期之后发布，则加入队列
				Integer postDate = Integer
						.parseInt(comment.get("postDate") == null ? "0"
								: comment.get("postDate").getAsString()
										.replace("-", ""));
				Integer beforedate = Integer.parseInt(date.replace("-", ""));
				if (postDate >= beforedate) {
					comments.add(comment);
				} else {
					stop = true;
					break;
				}
			}
			if (!doc.select(".NextPage").isEmpty()) {
				nextPage();
			} else {
				stop = true;
			}
		}

		return comments;
	}

	private void nextPage() {
		// TODO Auto-generated method stub
		pageno++;
	}

	private JsonObject fetchComment(Element element) {
		String username = element.select(".pic p.name a").text(); // 评论用户
		String score = getScore(element);
		Integer price = getPrice(element);
		Integer taste = getTaste(element);
		Integer eviroment = getEviroment(element);
		Integer service = getService(element);
		String content = getContent(element);
		String postDate = getPostDate(element);
		JsonObject comment = new JsonObject();
		comment.addProperty("username", username);
		comment.addProperty("score", score);
		comment.addProperty("price", price);
		comment.addProperty("taste", taste);
		comment.addProperty("eviroment", eviroment);
		comment.addProperty("service", service);
		comment.addProperty("content", content);
		comment.addProperty("postDate", postDate);
		return comment;
	}

	private String getPostDate(Element element) {
		String nowyear = date.substring(0, 4);
		String result = element.select(".content .misc-info .time").text();
		if (result.indexOf("更新") > -1) {
			result = result.split("更新于")[1];
			result = result.substring(0, result.indexOf(" "));
		}
		if (result.length() < 6) {
			result = nowyear + "-" + result;
		} else {
			String preyear = nowyear.substring(0, 2);
			if (Integer.parseInt(preyear + result.substring(0, 2)) > Integer
					.parseInt(nowyear)) {
				preyear = String.valueOf(Integer.parseInt(preyear) - 1);
			}
			result = preyear + result;
		}
		return result;
	}

	private String getContent(Element element) {
		String result = element.select(".content .comment-txt .J_brief-cont")
				.text();
		return result;
	}

	private Integer getService(Element element) {
		Elements elements = element
				.select(".content .user-info .comment-rst .rst");
		Integer score = 0;
		for (Element ele : elements) {
			if (ele.text().indexOf("服务") > -1) {
				score = PatternHelper.getIntNum(ele.text());
				break;
			}
		}
		return score;
	}

	private Integer getEviroment(Element element) {
		Elements elements = element
				.select(".content .user-info .comment-rst .rst");
		Integer score = 0;
		for (Element ele : elements) {
			if (ele.text().indexOf("环境") > -1) {
				score = PatternHelper.getIntNum(ele.text());
				break;
			}
		}
		return score;
	}

	private Integer getTaste(Element element) {
		Elements elements = element
				.select(".content .user-info .comment-rst .rst");
		Integer score = 0;
		for (Element ele : elements) {
			if (ele.text().indexOf("口味") > -1) {
				score = PatternHelper.getIntNum(ele.text());
				break;
			}
		}
		return score;
	}

	private Integer getPrice(Element element) {
		String price = element.select(".content .user-info .comm-per").text();
		return PatternHelper.getIntNum(price);
	}

	private String getScore(Element element) {
		String result = element.select(".content .user-info .item-rank-rst")
				.attr("title");
		return result;
	}

	public void setCommentUrlFromStoreUrl(String storeUrl) {
		if (storeUrl.endsWith("/")) {
			storeUrl = storeUrl.substring(0, storeUrl.length() - 1);
		}
		this.commentUrl = storeUrl + "/review_all";
	}

	public String getCommentUrl() {
		return commentUrl;
	}

	public void setCommentUrl(String commentUrl) {
		this.commentUrl = commentUrl;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getStoreId() {
		return storeId;
	}

	public void setStoreId(String storeId) {
		this.storeId = storeId;
	}

}
